﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Enter.Common
{
   public class RequestStrHelper
    {
        public static int GetIntFromRequest(string key)
        {
            return GetIntFromRequest(key, 0);
        }

        public static int GetIntFromRequest(string key, int num)
        {
            return GetIntFromRequest(HttpContext.Current, key, num);
        }

        public static int GetIntFromRequest(HttpContext content, string key, int num)
        {
            int length = -2147483648;
            string valueFromRequest = GetValueFromRequest(content, key);
            if (SafeString.IsNull(valueFromRequest))
            {
                return num;
            }
            length = valueFromRequest.IndexOf(",");
            if (length > 0)
            {
                valueFromRequest = valueFromRequest.Substring(0, length);
            }
            length = valueFromRequest.IndexOf("#");
            if (length > 0)
            {
                valueFromRequest = valueFromRequest.Substring(0, length);
            }
            try
            {
                length = Convert.ToInt32(valueFromRequest);
            }
            catch
            {
                return num;
            }
            return length;
        }
        public static decimal GetDecimalFromRequest(string key)
        {
            return GetDecimalFromRequest(key, 0);
        }

        public static decimal GetDecimalFromRequest(string key, int num)
        {
            return GetDecimalFromRequest(HttpContext.Current, key, num);
        }

        public static decimal GetDecimalFromRequest(HttpContext content, string key, int num)
        {
            int length = -2147483648;
            string valueFromRequest = GetValueFromRequest(content, key);
            if (SafeString.IsNull(valueFromRequest))
            {
                return num;
            }
            length = valueFromRequest.IndexOf(",");
            if (length > 0)
            {
                valueFromRequest = valueFromRequest.Substring(0, length);
            }
            length = valueFromRequest.IndexOf("#");
            if (length > 0)
            {
                valueFromRequest = valueFromRequest.Substring(0, length);
            }
            try
            {
               return Convert.ToDecimal(valueFromRequest);
            }
            catch
            {
                return num;
            }
            return num;
        }
        public static string GetStringFromRequest(string key)
        {
            return GetStringFromRequest(HttpContext.Current, key, 0, string.Empty);
        }

        public static string GetStringFromRequest(string key, int leg)
        {
            return GetStringFromRequest(HttpContext.Current, key, leg, string.Empty);
        }

        public static string GetStringFromRequest(string key, string deftxt)
        {
            return GetStringFromRequest(HttpContext.Current, key, 0, deftxt);
        }

        public static string GetStringFromRequest(string key, int leg, string deftxt)
        {
            return GetStringFromRequest(HttpContext.Current, key, leg, deftxt);
        }

        public static string GetStringFromRequest(HttpContext content, string key, int leg, string deftxt)
        {
            string valueFromRequest = GetValueFromRequest(content, key);
            if (SafeString.IsNull(valueFromRequest))
            {
                return deftxt;
            }
            if ((leg > 0) && (valueFromRequest.Length > leg))
            {
                valueFromRequest = valueFromRequest.Substring(0, leg);
            }
            return valueFromRequest;
        }
        public static bool GetBooleanFromRequest(string key)
        {
            return GetBooleanFromRequest(HttpContext.Current, key);
        }

        public static bool GetBooleanFromRequest(HttpContext content, string key)
        {
            bool flag = false;
            string valueFromRequest = GetValueFromRequest(content, key);
            if (SafeString.IsNull(valueFromRequest))
            {
                return flag;
            }
            if (SafeConvert.ToInt32(valueFromRequest) < 1)
            {
                return flag;
            }
            return true;
        }
        private static string GetValueFromRequest(HttpContext content, string key)
        {
            string str = content.Request.Form[key];
            if (SafeString.IsNull(str))
            {
                str = content.Request.QueryString[key];
            }
            if (SafeString.IsNull(str))
            {
                str = content.Request.Headers[key];
            }
            Encoding e = Encoding.UTF8;
            return HttpUtility.UrlDecode(str, e);
        }
    }
}
